//
//  ItemNineViewController.m
//  UI12豆瓣大项目
//
//  Created by 李奇 on 15/11/5.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//
#define WIDTH  [[UIScreen mainScreen] bounds].size.width
#define HEIGHT [[UIScreen mainScreen] bounds].size.height

#import "ItemNineViewController.h"
#import "Activity.h"
#import "MBProgressHUD.h"
#import "AppTools.h"
#import "ItemCollectionViewCell.h"
#import "FilmDetailViewController.h"
@interface ItemNineViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic,retain) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic,retain) MBProgressHUD *hub;
@property (nonatomic,retain) Activity *activity;
@property (nonatomic,retain) NSMutableArray *filmArray;
@property (nonatomic,retain) UILabel *title1;
@property (nonatomic,retain) UIImageView *pictureV;
@property (nonatomic,retain) UICollectionView *collectView;
@end

@implementation ItemNineViewController
- (void)dealloc
{
    [_filmArray release];
    [_hub release];
    [_flowLayout release];
    [_activity release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置透明度
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"电影";
    
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.flowLayout.minimumLineSpacing = ((HEIGHT - 49 - 64) - 170 * 3) / 4;
    self.flowLayout.minimumInteritemSpacing = (WIDTH - 110 * 3) / 4;
    self.flowLayout.itemSize = CGSizeMake(110, 170);
    self.flowLayout.sectionInset = UIEdgeInsetsMake(((HEIGHT - 49 - 64) - 170 * 3)/4, (WIDTH - 110 * 3) / 4, ((HEIGHT - 49 - 64) - 170 * 3)/4, (WIDTH - 110 * 3) / 4);
    
    
    
    
    
    
    //加载菊花
    self.hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.hub show:YES];
    self.hub.labelText = @"请耐心等待....";
    //请求网络
    [AppTools getDataWithkey:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php" block:^(id result) {
        //        NSLog(@"%@",result);
        NSMutableDictionary *bigDic = result;
        NSString *error_code = [bigDic objectForKey:@"error_code"];
        NSString *reason = [bigDic objectForKey:@"reason"];
        NSMutableArray *array = [bigDic objectForKey:@"result"];
        if ([reason isEqualToString:@"success"]) {
            self.filmArray = [NSMutableArray array];
            for (NSMutableDictionary *dic in array) {
                self.activity = [[Activity alloc] init];
                [self.activity setValuesForKeysWithDictionary:dic];
                [self.filmArray addObject:self.activity];
                [_activity release];
                [self.hub hide:YES];
            }
        }
        else{
            NSLog(@"请求失败");
        }
#warning 顺序!!
        
        self.collectView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 49 - 64) collectionViewLayout:self.flowLayout];
        self.collectView.backgroundColor = [UIColor whiteColor];
        self.collectView.delegate = self;
        self.collectView.dataSource = self;
        
        //注册
        [self.collectView registerClass:[ItemCollectionViewCell class] forCellWithReuseIdentifier:@"Item"];
        
        [self.view addSubview:self.collectView];
        [_collectView release];
        
    }];
    // Do any additional setup after loading the view.
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.filmArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ItemIdentifer = @"Item";
    ItemCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ItemIdentifer forIndexPath:indexPath];
    cell.activity = [self.filmArray objectAtIndex:indexPath.row];
//    cell.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    FilmDetailViewController *filmDVC = [[FilmDetailViewController alloc] init];
    //需要做个小初始化!!!
    Activity *activity = [self.filmArray objectAtIndex:indexPath.row];
    //传ID
    filmDVC.movieID = activity.movieId;
    [self.navigationController pushViewController:filmDVC animated:YES];
    [filmDVC release];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
